package jpavlich.cardsorter;

import java.io.File;
import java.io.FileFilter;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExperimentsListModel extends FileListModel {

	public int getSize() {
		return getValues().length;
	}

	public Object getElementAt(int index) {
		return getValues()[index];
	}

	public File[] getValues() {
		if (files == null) {
			updateValues();
		}
		return files;

	}

	public String getNewExperimentID() {
		File[] exps = getValues();
		if (exps == null || exps.length == 0) {
			return "Experimento 001";
		} else {
			String lastExp = exps[exps.length - 1].getName();
			Pattern r = Pattern.compile("([0-9]+)");
			Matcher m = r.matcher(lastExp);
			if (m.find()) {
				Integer expNum = Integer.valueOf(m.group());
				int pos = m.start();
				String prefix = "";
				if (pos > 0) {
					prefix = lastExp.substring(0, pos - 1) + " ";
				}
				DecimalFormat df = new DecimalFormat("000");
				String num = df.format(expNum + 1);
				return prefix + num;
			}
			return lastExp + " 001";
		}
	}

	@Override
	protected FileFilter getFileFilter() {
		return new FileFilter() {

			@Override
			public boolean accept(File f) {
				return f.isDirectory();
			}
		};
	}

	@Override
	public File getBaseFolder() {
		return Persistence.EXPERIMENTS_PATH;
	}

}
